package com.zhihuiweijian.demo.controller;

import com.zhihuiweijian.demo.model.User;
import com.zhihuiweijian.demo.model.Waste;
import com.zhihuiweijian.demo.service.UserService;
import com.zhihuiweijian.demo.service.WasteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("waste")
@Api(tags={"医废操作接口"})
public class WasteController {
    @Autowired
    private WasteService wasteService;
    /**\
     * 格式化前台传来的日期数据
     * @param request
     * @param binder
     */
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }



    @ApiOperation(value="接收秤传来的初始信息", notes="医废ID,医院ID，科室名称，负责人，医废类型，重量",produces = "application/json")
    @GetMapping(value="/add")
    public Map<String, Object> add(Waste waste) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            waste.setTime1(format.parse(format.format(cal.getTime())));
            wasteService.insert(waste);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }






    @ApiOperation(value="更新医院回收信息", notes="医废ID,回收人员Id，回收人员姓名",produces = "application/json")
    @GetMapping(value="/updateCollect")
    public Map<String, Object> updateCollect(Integer id ,Integer collectManId,String collectManName) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            Date date = format.parse(format.format(cal.getTime()));
            Waste waste = wasteService.findById(id);
            waste.setTime2(date);
            waste.setCollectmanid(collectManId);
            waste.setCollectmanname(collectManName);
            wasteService.updateCollect(id,collectManId,collectManName,date);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="更新医院仓库信息", notes="医废ID,仓库人员Id，仓库人员姓名",produces = "application/json")
    @GetMapping(value="/updateWareHouse")
    public Map<String, Object> updateWareHouse(Integer id ,Integer warehouseManId,String warehouseManName) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            Date date = format.parse(format.format(cal.getTime()));
            Waste waste = wasteService.findById(id);
            waste.setTime2(date);
            waste.setCollectmanid(warehouseManId);
            waste.setCollectmanname(warehouseManName);
            wasteService.updateWareHouse(id,warehouseManId,warehouseManName,date);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="更新运输信息", notes="医废ID,运输人员Id，运输人员姓名",produces = "application/json")
    @GetMapping(value="/updateTransfer")
    public Map<String, Object> updateTransfer(Integer id ,Integer transferManId,String transferManName) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            Date date = format.parse(format.format(cal.getTime()));
            Waste waste = wasteService.findById(id);
            waste.setTime2(date);
            waste.setCollectmanid(transferManId);
            waste.setCollectmanname(transferManName);
            wasteService.updateTransfer(id,transferManId,transferManName,date);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="更新销毁信息", notes="医废ID,销毁人员Id，销毁人员姓名",produces = "application/json")
    @GetMapping(value="/updateRuin")
    public Map<String, Object> updateRuin(Integer id ,Integer ruinManId,String ruinManName) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            Date date = format.parse(format.format(cal.getTime()));
            Waste waste = wasteService.findById(id);
            waste.setTime2(date);
            waste.setCollectmanid(ruinManId);
            waste.setCollectmanname(ruinManName);
            wasteService.updateRuin(id,ruinManId,ruinManName,date);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }


    @ApiOperation(value="更新异常信息", notes="医废ID,异常人员Id，异常人员姓名",produces = "application/json")
    @GetMapping(value="/updateError")
    public Map<String, Object> updateError(Integer id ,Integer errorManId,String errorManName) {
        Map<String, Object> map = new HashMap();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            Date date = format.parse(format.format(cal.getTime()));
            Waste waste = wasteService.findById(id);
            waste.setTime2(date);
            waste.setCollectmanid(errorManId);
            waste.setCollectmanname(errorManName);
            wasteService.updateError(id,errorManId,errorManName,date);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @ApiOperation(value="根据医废ID查询单条医废详情", notes="",produces = "application/json")
    @GetMapping(value="/findById")
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> map = new HashMap();
        try {
            map.put("data",wasteService.findById(id));
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

}
